//
//  JSMFooterProvinceDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMFooterProvinceDetailsViewController.h"
#import "JSMLeavingDiscoverMoreDetailsModel.h"
#import "JSMLeavingDiscoverTableViewCell.h"
#import "JSMLeavingDiscoverMoreDetailsInfoModel.h"
#import "JSMProvinceDetailsDetailsViewController.h"

@interface JSMFooterProvinceDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) BaseTableView *provinceDetailsTableView;
@property(nonatomic, strong) NSMutableArray *discoverMoreDetailsListMarr;
@property(nonatomic , strong) NSMutableArray *discoverMoreDetailsDescriptionMarr;
@end

@implementation JSMFooterProvinceDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.discoverMoreModel.name;
    [self createTableView];
    [self createData];
}
- (void)createData {
    if (self.formView == FromViewDiscoverViewController) {
        NSString *strUrl = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/trail/type/%@?&app_version=4.3.3&page_size=20&api_version=1&page=1&device_type=2&access_token=&type_id=%@", self.discoverMoreModel.modelId];
        [AFNetworkingTool getUrl:strUrl hudSuperView:self.view result:JSON success:^(id result) {
            if (result != nil) {
                JSMLeavingDiscoverMoreDetailsModel *moreDetails = [[JSMLeavingDiscoverMoreDetailsModel alloc] init];
                [moreDetails setValuesForKeysWithDictionary:result];
                self.discoverMoreDetailsListMarr = [NSMutableArray array];
                self.discoverMoreDetailsListMarr = moreDetails.trailsMarr;
                self.discoverMoreDetailsDescriptionMarr = [NSMutableArray array];
                self.discoverMoreDetailsDescriptionMarr = moreDetails.typeMarr;
                if (self.discoverMoreDetailsListMarr.count > 0 && self.discoverMoreDetailsDescriptionMarr.count > 0) {
                    [self.provinceDetailsTableView reloadData];
                }
            }

            
        } failure:^(NSError *error) {
            
        }];
    } else {
        NSString *url = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/trails?&page_size=20&area_id=&page=1&search=%@&device_type=2&search_in=1&app_version=4.3.3&api_version=1&access_token=", [self.provinceName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [AFNetworkingTool getUrl:url hudSuperView:self.view result:JSON success:^(id result) {
            if (result != nil) {
                JSMLeavingDiscoverMoreDetailsModel *moreDetails = [[JSMLeavingDiscoverMoreDetailsModel alloc] init];
                [moreDetails setValuesForKeysWithDictionary:result];
                self.discoverMoreDetailsListMarr = [NSMutableArray array];
                self.discoverMoreDetailsListMarr = moreDetails.trailsMarr;
                [self.provinceDetailsTableView reloadData];
            }
        } failure:^(NSError *error) {
            
        }];
    }
}

- (void)createTableView {
    self.provinceDetailsTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.provinceDetailsTableView];
    self.provinceDetailsTableView.delegate = self;
    self.provinceDetailsTableView.dataSource = self;
    self.provinceDetailsTableView.rowHeight = 200;
    [self.provinceDetailsTableView registerClass:[JSMLeavingDiscoverTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.discoverMoreDetailsListMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JSMLeavingDiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:[self.discoverMoreDetailsListMarr[indexPath.row] cover]] placeholderImage:PlaceHolderImage];
    cell.titleLabel.backgroundColor = [UIColor colorWithRed:0.341 green:0.340 blue:0.355 alpha:0.550];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@·%@", [self.discoverMoreDetailsListMarr[indexPath.row] destination], [self.discoverMoreDetailsListMarr[indexPath.row] name]];
    cell.titleLabel.textColor = [UIColor whiteColor];
    cell.titleLabel.font = [UIFont boldSystemFontOfSize:19];
    cell.infoLabel.backgroundColor = [UIColor colorWithRed:0.378 green:0.377 blue:0.394 alpha:0.550];
    cell.infoLabel.text = [self.discoverMoreDetailsListMarr[indexPath.row] descriptionStr];
    cell.infoLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.infoLabel.textColor = [UIColor whiteColor];
    cell.infoLabel.numberOfLines = 0;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JSMProvinceDetailsDetailsViewController *details = [[JSMProvinceDetailsDetailsViewController alloc] init];
    details.infoModel = self.discoverMoreDetailsListMarr[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
